/**
 * http://www.jldh.net/house/loupan.html
 * 敦化信息网新楼盘
 * sunny 2017-12-21
 */

import { Base, Item } from "../base";

// 工具类
const utils = require('../../utils');

// 敦化信息网新楼盘
const dhxxw_url = "http://www.jldh.net/house/loupan.html";

class dhxxw_spider extends Base {
    form: any;
    firstPage: string;
    cacheContent: string;
    constructor() {
        super();
        this.name = "敦化信息网-敦化房产网";
        this.enabled = true;
        this.encode = "utf-8";
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "dhfcxx";
        this.collectionShowName="房地产信息";
        this.keyField = "title";
       
    }
    getPageCount(): number {
        return 2;
    }
    getItems(pageIndex: number): Item[] {
        let url = `http://www.jldh.net/house/loupan.html?PageNo=${pageIndex + 1}`;
        let content: string = this.spider.getSync(url);
        let $ = this.cheerio.load(content);
        let urls: any[] = [];
        $(".con h4 a").each((index, ele) => {
            let url = 'http://www.jldh.net/house/' + $(ele).attr("href");
            urls.push(url);
            console.log(url);
        });
        let result: Item[] = [];
        urls.forEach((url) => {
            let item = this.extractItems(url);
            result.push(item);
        });
        return result;
    }
    private extractItems(url: string): Item {
        let content = this.spider.getSync(url);
        let item: Item = this.buildItem();
        let $ = this.cheerio.load(content);
        (item as any).url = url;
        (item as any).title = $(".house_name h3").text();
        $(".fr dd").each((index, ele) => {
            let text = $(ele).text();
            if (index == 0) {
                (item as any)['单价'] = text; //4600元/平米
                if (/(\d+)元/.test(text)) {
                    (item as any)['单价'] = RegExp.$1;
                }
            } else if (index == 1) {
                (item as any)['开发商'] = text;
            } else if (index == 2) {
                (item as any)['地址'] = text;
            } else if (index == 3) {
                (item as any)['时间'] = text;
                if (/(\d+)年/.test(text)) {
                    (item as any)['年度'] = RegExp.$1;
                }
            }
        });
        let detailUrl = url.replace('loupan', 'xq');
        content = this.spider.getSync(detailUrl);
        $ = this.cheerio.load(content);
        let keyMap: any = {};
        $(".lp_info .clearfix dt").each((index, ele) => {
            let text = $(ele).text();
            keyMap[index] = text;
            (item as any)[text] = '';
        });
        $(".lp_info .clearfix dd").each((index, ele) => {
            let text = $(ele).text();
            let key = keyMap[index];
            (item as any)[key] = text;
        });
        let dj = (item as any)['单价'] || 0;
        let mj = (item as any)['总建筑面积：'] || 0;
        let zj = dj * mj;
        (item as any)['总价'] = zj;
        console.log(JSON.stringify(item));
        return item;
    }
}

export = new dhxxw_spider();
